﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using PlanningGIS.Statistics;
using PlanningGIS.Win.Controls;

namespace PlanningGIS.Statistics
{
    public partial class FrmCustomStat : Office2007Form 
    {
        private static string strConfig =Application.StartupPath +
                    (System.Configuration.ConfigurationManager.AppSettings.Get("Folder") == null || System.Configuration.ConfigurationManager.AppSettings.Get("Folder").Trim() == "" ? "" : ("\\" + System.Configuration.ConfigurationManager.AppSettings.Get("Folder"))) +
                    "\\Config\\StatConfig.xml";
        private List<StatLayer> pLyrs = null;
        private StatLayer pCurrentLyr = null;

        public  StatLayer ReportStatLayer
        {
            get { return pCurrentLyr; }
        }

        public string ReportName
        {
            get { return cboReport.Text; }
        }

        public FrmCustomStat()
        {
            InitializeComponent();
        }

        private void FrmCustomStat_Load(object sender, EventArgs e)
        {
            if (!File.Exists(strConfig))
            {
                MsgHelper.ShowInfomationMsgbox("报表统计的配置文件不存在！");
                Close();
            }
            pLyrs = StatFactory.GetLayersByConfigxml(strConfig);
            if (pLyrs == null || pLyrs.Count == 0)
            {
                MsgHelper.ShowInfomationMsgbox("报表统计配置文件不为空！");
                Close();
            }
            foreach (StatLayer plyr in pLyrs)
            {
                if (plyr.ReportNames == null || plyr.ReportNames.Count == 0)
                {
                    continue;
                }
                else if ((plyr.ReportNames.Count == 1) && !(plyr.ReportNames[0].Display))
                {
                    continue;
                }
                else
                {
                    cboLayer.Items.Add(plyr.Value);
                }
            }
            if (cboLayer.Items.Count > 0)
            {
                cboLayer.SelectedIndex = 0;
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            string reportname = cboReport.Text.Trim();
            if(reportname ==null ||reportname.Length ==0)
            {
                MsgHelper.ShowInfomationMsgbox("请选择统计类型！");
                return;
            }

            pCurrentLyr.StatCodes.Clear();
            List<StatCode> codes = new List<StatCode>();
            for (int i = 0; i < tvwField.Nodes.Count; i++)
            {
                TreeNode pNode = tvwField.Nodes[i];
                StatCode pCode=pNode.Tag as StatCode;
                if (pNode.Checked)
                {
                    pCode.Check = true;
                }
                else
                {
                    pCode.Check = false;
                }
                codes.Add(pCode);
            }
            pCurrentLyr.StatCodes = codes;
            DialogResult = DialogResult.OK;
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void cboLayer_SelectedIndexChanged(object sender, EventArgs e)
        {
            string text = cboLayer.Text.Trim();
            GetStatlayerByValue(text);
            cboReport.Items.Clear();
            if (pCurrentLyr.ReportNames != null)
            {
                foreach (ReportName name in pCurrentLyr.ReportNames)
                {
                    if (name.Display)
                    {
                        cboReport.Items.Add(name.Key);
                    }
                }
                if (cboReport.Items.Count > 0)
                {
                    cboReport.SelectedIndex = 0;
                }
            }
            tvwField.Nodes.Clear();
            if (pCurrentLyr.StatCodes != null)
            {
                foreach (StatCode code in pCurrentLyr.StatCodes)
                {
                    TreeNode pNode=tvwField.Nodes.Add(code.Value);
                    pNode.Tag = code;
                    if (code.Check)
                    {
                        pNode.Checked = true;
                    }
                    else
                    {
                        pNode.Checked = false;
                    }
                }
            }
            
        }

        private void GetStatlayerByValue(string text)
        {
            foreach (StatLayer lyr in pLyrs)
            {
                if (lyr.Value == text)
                {
                    pCurrentLyr = lyr;
                    return;
                }
            }
        }

        private void tvwField_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ShowTreeContextmenu(sender as TreeView);
            }
        }

        private void ShowTreeContextmenu(TreeView pTree)
        {
            ContextMenuStrip tvwMenuStrip = new ContextMenuStrip();
            ToolStripMenuItem pSelAllTool = new ToolStripMenuItem("全选");
            pSelAllTool.Click += delegate(object sender1, EventArgs e1)
            {
                foreach (TreeNode pNode in pTree.Nodes)
                {
                    if (pNode.Checked == false)
                        pNode.Checked = true;
                }
            };
            ToolStripMenuItem pSelNoTool = new ToolStripMenuItem("全部不选");
            pSelNoTool.Click += delegate(object sender1, EventArgs e1)
            {
                foreach (TreeNode pNode in pTree.Nodes)
                {
                    if (pNode.Checked)
                        pNode.Checked = false;
                }
            };
            tvwMenuStrip.Items.AddRange(new ToolStripItem[]
                                                {
                                                    pSelAllTool,
                                                    pSelNoTool 
                                                });
            pTree.ContextMenuStrip = tvwMenuStrip;
        }
    }
}
